package com.atguigu.eduService.controller.front;


import com.atguigu.common.Result;
import com.atguigu.eduService.client.OrderClient;
import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.chapter.Chapter;
import com.atguigu.eduService.entity.frontvo.CourseFrontVo;
import com.atguigu.eduService.entity.frontvo.CourseWebVo;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.utils.JwtUtils;
import com.atguigu.vo.WebCourseInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduService/courseFront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1 条件查询带分页查询课程
    @PostMapping("getFrontCourseList")
    public Result getFrontCourseList(@RequestParam("current") Long current,
                                     @RequestParam("size") Long size,
                                     @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(current, size);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        //返回分页所有数据
        return Result.ok().data(map);
    }

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<Chapter> chapterVideoList = chapterService.getAllChaptersByCourseId(courseId);

        boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Result.ok().data("course",courseWebVo).data("chapters",chapterVideoList).data("isBuy",buyCourse);
    }

    @GetMapping("/getWebCourseInfoVo/{courseId}")
    public WebCourseInfoVo getWebCourseInfoVo(@PathVariable String courseId) {
        WebCourseInfoVo webCourseInfoVo = new WebCourseInfoVo();
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(courseId);
        BeanUtils.copyProperties(baseCourseInfo,webCourseInfoVo);
        return webCourseInfoVo;

    }
}












